package com.lyj.validates.validates;

import com.google.common.base.Strings;
import com.lyj.validates.dto.UserDto;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.*;

/**
 * 用户dto类校验
 * @author:liyajie
 * @createTime:2022/1/20 10:18
 * @version:1.0
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Constraint(validatedBy = UserValidator.UserValidateValidator.class)
public @interface UserValidator {

    // 校验未通过时的返回信息
    String message() default "id不能为空";

    // 以下两行为固定模板
    Class<?>[] groups() default {};

    // 约束注解的有效负载
    Class<? extends Payload>[] payload() default {};

    class UserValidateValidator implements ConstraintValidator<UserValidator, UserDto> {

        public boolean isValid(UserDto testDto, ConstraintValidatorContext context) {
            boolean isValidate = false;
            //校验逻辑
            if(!Strings.isNullOrEmpty(testDto.getId())){
                isValidate = true;
            }
            return isValidate;
        }
    }
}
